package com.joinlabs.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.joinlabs.entity.Log;
import com.joinlabs.entity.Topic;
import com.joinlabs.entity.User;
import com.joinlabs.entity.Weibo;
import com.joinlabs.service.ILogService;
import com.joinlabs.service.ITopicService;
import com.joinlabs.service.IUserService;
import com.joinlabs.service.IWeiboService;

@Controller
@RequestMapping("/searchlogic")
@SessionAttributes(names = {"weibolist","userlist","loglist","topiclist","context"})
public class SearchLogicAction {
	@Resource
	private IWeiboService weiboService;
	
	@Resource
	private IUserService userService;
	
	@Resource
	private ILogService logService;
	
	@Resource
	private ITopicService topicService;
	
	//搜索微博
	@RequestMapping("/weibo")
	public ModelAndView index(@RequestParam(value="searchText")String context,ModelMap map) {
		System.out.println("||||||||||||"+context);
		if(context == "") {
			List<Weibo> weiboList = weiboService.selectAllWeibo();
			map.addAttribute("weibolist",weiboList);
			map.addAttribute("context", context);
			return new ModelAndView("/search/allSearch");
		}else {
			List<Weibo> weiboList = weiboService.selectSomeWeibo(context);
			System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!"+weiboList);
			map.addAttribute("weibolist",weiboList);
			map.addAttribute("context", context);
			return new ModelAndView("/search/allSearch");
		}
	}
	
	//搜索用户
	@RequestMapping("/find")
	public ModelAndView find(@RequestParam(value="searchText")String context,ModelMap map) {
		System.out.println("||||||||||||"+context);
		if(context == "") {
			List<User> userList = userService.selectAllUser();
			map.addAttribute("userlist", userList);
			map.addAttribute("context", context);
			return new ModelAndView("/search/findPeopleSearch");
		}else {
			List<User> userList = userService.selectSomeUser(context);
			System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!"+userList);
			map.addAttribute("userlist",userList);
			map.addAttribute("context", context);
			return new ModelAndView("/search/findPeopleSearch");
		}
	}
	
	//搜索文章
	@RequestMapping("/article")
	public ModelAndView article(@RequestParam(value="searchText")String context,ModelMap map) {
		System.out.println("||||||||||||"+context);
		if(context == "") {
			List<Log> logList = logService.selectAllLog();
			map.addAttribute("loglist", logList);
			map.addAttribute("context", context);
			return new ModelAndView("/search/articleSearch");
		}else {
			List<Log> logList = logService.selectSomeLog(context);
			System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!"+logList);
			map.addAttribute("loglist", logList);
			map.addAttribute("context", context);
			return new ModelAndView("/search/articleSearch");
		}
	}
	
	//搜索话题
	@RequestMapping("/topic")
	public ModelAndView topic(@RequestParam(value="searchText")String context,ModelMap map) {
		System.out.println("||||||||||||"+context);
		if(context == "") {
			List<Topic> topicList = topicService.selectAllTopic();
			map.addAttribute("topiclist", topicList);
			map.addAttribute("context", context);
			return new ModelAndView("/search/topicSearch");
		}else {
			List<Topic> topicList = topicService.selectSometopic(context);
			System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!"+topicList);
			map.addAttribute("topiclist", topicList);
			map.addAttribute("context", context);
			return new ModelAndView("/search/topicSearch");
		}
	}
}
